package com.simplenix.nicasio.misc;

import com.simplenix.nicasio.def.Module;
import com.simplenix.nicasio.sys.SystemDef;
import java.io.File;
import java.util.Hashtable;
import java.util.List;

/**
 *
 * @author fronald
 */
public class Internationalization {

	private static Internationalization itz = null;

	private Hashtable<String, String> strings = new Hashtable<String, String>();

	public static Internationalization getInstance() throws AppException {
		if (itz == null) {
			itz = new Internationalization();
		}
		return itz;
	}

	private Internationalization() throws AppException {
		try {
			String resource = "WEB-INF/config/lang/sys/" + AppConfiguration.getInstance().getDefaultLanguage() + ".properties";
			this.strings.putAll((new PropertiesReader(resource)).getPropertiesAsMap());
			resource = "WEB-INF/config/lang/" + AppConfiguration.getInstance().getDefaultLanguage() + ".properties";
			File f = new File(SystemDef.getInstance().getAppPath() + "/" + resource);
			if (f.exists()) {
				this.strings.putAll((new PropertiesReader(resource)).getPropertiesAsMap());
			}
			List<Module> modules = SystemDef.getInstance().getModules().getModulesList();
			for (Module module : modules) {
				resource = "WEB-INF/config/lang/" + module.getName() + "/" + AppConfiguration.getInstance().getDefaultLanguage() + ".properties";
				f = new File(SystemDef.getInstance().getAppPath() + "/" + resource);
				if (f.exists()) {
					this.strings.putAll((new PropertiesReader(resource)).getPropertiesAsMap());
				}
			}
		} catch (Exception e) {
			throw new AppException(e);
		}
	}

	public String getString(String key) {
		if (this.strings.containsKey(key)) {
			return this.strings.get(key);
		} else {
			return key;
		}
	}

}
